package org.zsk.cfl.listerner;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 多实例执行监听器
 * </p>
 *
 * @author zsk
 * @date 2020/11/10 上午8:15
 */
@Slf4j
@Component("multiExecutionListener")
public class MultiExecutionListener implements ExecutionListener, ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        RuntimeService runtimeService = applicationContext.getBean(RuntimeService.class);
        //同意
        runtimeService.setVariable(delegateExecution.getProcessInstanceId(), MultiConstants.ACCEPTED, 0);
        //拒绝
        runtimeService.setVariable(delegateExecution.getProcessInstanceId(), MultiConstants.REJECTED, 0);
        //弃权
        runtimeService.setVariable(delegateExecution.getProcessInstanceId(), MultiConstants.ABSTAINED, 0);
        log.debug("多实例执行监听器，设置变量：（同意）accepted-0 （拒绝）rejected-0 （弃权）abstained-0");
    }

    @Override
    public void setApplicationContext(ApplicationContext agr0) throws BeansException {
        applicationContext = agr0;
    }
}

